package org.anderground.editor.gui.menubar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

import org.anderground.editor.gui.MainWindow;
import org.anderground.editor.model.EditorModel;

/**
 * 
 * @author Ilya Shurigyn
 * 
 */
public class FileMenu extends JMenu {
	public FileMenu(MainWindow parent) {

		super("File");
		add(new NewMapMenuItem(parent));
		add(new OpenFileMenuItem(parent));
		add(new SaveMapMenuItem());
		// ---
		add(new JSeparator());
		add(new ImportMapMenu(parent));
		// ---
		add(new JSeparator());
		add(new ExitMenuItem(parent));

	}
}

/**
 * 
 * @author Ilya Shurigyn
 * 
 */
class ExitMenuItem extends JMenuItem {
	public ExitMenuItem(final MainWindow parent) {
		super("Exit");
		addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				parent.onClose();
			}

		});
	}
}

class NewMapMenuItem extends JMenuItem {
	public NewMapMenuItem(final MainWindow parent) {
		super("New map");
		addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (MainWindow
						.askForSave(
								"Do you want to crate new file without saving current?",
								parent) != JOptionPane.CANCEL_OPTION)
					EditorModel.getInstance().clear();
			}

		});
	}
}
